using System;
using System.Collections.Generic;
using System.Text;

using RecruitmentSystem.Common;
using RecruitmentSystem.DataAccess;

namespace RecruitmentSystem.Business
{
    public class NewsManager
    {
        private NewsAccessor newsAccessor;

        public NewsManager()
        {
            newsAccessor = new NewsAccessor();
        }

        public bool CreateNews(News news)
        {
            bool result = false;

            try
            {
                result = newsAccessor.AddNews(news);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in create news", ex);
            }

            return result;
        }

        public List<News> GetNewsByCategoryId(int? categoryId, int pageSize, int pagecurrent)
        {
            List<News> allNews = new List<News>();

            try
            {
                allNews = newsAccessor.GetNewsByCategoryId(categoryId, pageSize, pagecurrent);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get news by categoryId", ex);
            }
            return allNews;
        }

        public bool Remove(int newsId)
        {
            bool result = false;

            try
            {
                result = newsAccessor.DeleteNewsById(newsId);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in remove news by Id", ex);
            }

            return result;
        }

        public bool Reomve(List<int> newsIds)
        {
            bool result = false;

            if (newsIds != null && newsIds.Count > 0)
            {
                try
                {
                    foreach (int id in newsIds)
                    {
                        Remove(id);
                    }

                    result = true;
                }
                catch (Exception ex)
                {
                    Logger.Error("Exception occurs in remove news by Id", ex);
                }
            }

            return result;
        }

        public bool UpdateNews(News news)
        {
            bool result = false;

            try
            {
                result = newsAccessor.UpdateNewsById(news);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in update news by Id", ex);
            }

            return result;
        }

        public int GetNewsCountByCategoryId(int? categoryId)
        {
            int count = 0;

            try
            {
                count = newsAccessor.GetNewsCountByCategoryId(categoryId);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get news count", ex);
            }

            return count;
        }

        public News GetNewsById(int id)
        {
            News news = new News();

            try
            {
                news = newsAccessor.GetNewsById(id);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get news by id", ex);
            }

            return news;
        }

        public List<Category> GetAllCategory(int? level)
        {
            List<Category> allCategory = new List<Category>();

            try
            {
                allCategory = newsAccessor.GetAllCategory(level);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get all category", ex);
            }

            return allCategory;
        }

        public bool CreateCategory(Category category)
        {
            bool result = false;

            try
            {
                result = newsAccessor.AddCategory(category);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in create category", ex);
            }

            return result;
        }

        public bool UpdateCategory(Category category)
        {
            bool result = false;

            try
            {
                result = newsAccessor.UpdateCategoryById(category);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in update category by Id", ex);
            }

            return result;
        }

        public Category GetCategory(int categoryId)
        {
            Category category = new Category();

            try
            {
                category = newsAccessor.GetCategoryById(categoryId);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get category by id", ex);
            }

            return category;
        }
    }
}
